package net.wanho.commons.factory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import net.wanho.commons.proxy.TransactionProxy;

/**
 * <工厂类>
 *
 * @author zj
 * @author zcf 简化工厂 通过class类型简单名称获得 该类型的实现类
 * @version [V1.0.0, 2017-4-10]
 * @version [V1.0.1, 2017-4-10]
 */
public class BeanFactory {

    private static Map<String, Object> objects = new HashMap<String, Object>();

    static {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(BeanFactory.class
                    .getClassLoader().getResourceAsStream("bean.properties")));
            String s = null;
            while ((s = br.readLine()) != null) {
                if (!"".equals(s) && !"#".equals(s.charAt(0))) {
                    String[] entry = s.split("=");
                    objects.put(entry[0], Class.forName(entry[1]).newInstance());
                } else {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError("ObjectFactory初始化错误" + e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static <T> T getBean(Class<T> cls) {
        String key = cls.getSimpleName();
        final Object obj = objects.get(key);
        if (key.contains("service") || key.contains("Service")) {
            return (T) TransactionProxy.createProxy(obj);
        }
        return (T) obj;
    }
}
